<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="800px"
    top="8vh"
    append-to-body
  >
    <el-container>
      <el-main style="max-height: 70vh">
        <!-- <el-row :gutter="20"> -->
        <el-form
          :model="dataForm"
          :rules="dataRule"
          ref="dataForm"
          @keyup.enter.native="dataFormSubmit()"
          label-width="130px"
        >
          <el-form-item
            label="指标分类"
            v-if="normLxs.length > 0 && normType == 'htrw_zb'&&normType !== 'rckh_zb'"
            prop="normFl"
          >
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normFl"
              placeholder="请选择指标分类"
              :disabled="disabled"
              @change="normflsChange()"
            >
              <el-option
                v-for="item in normFls"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="指标分类" v-if="normType == 'rckh_zb'" prop="normFl">
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normFl"
              placeholder="请选择指标分类"
              :disabled="disabled"
              @change="normflsChange()"
            >
              <el-option
                v-for="item in normFls1"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="指标名称" prop="normName">
            <el-input v-model="dataForm.normName" placeholder="指标名称" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item
            label="指标类型"
            v-if="
              normLxs.length > 0 &&
              (normType == 'htrw_zb' || normType == 'rckh_zb'||normType == 'rckh_zb')
            "
            prop="normLx"
          >
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normLx"
              placeholder="请选择指标类型"
            >
              <el-option
                v-for="item in normLxs"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            label="计算公式"
            v-if="normLxs.length > 0 && normType == 'htrw_zb'||normType !== 'rckh_zb'"
            prop="normGs"
          >
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normGs"
              placeholder="请选择计算公式"
              :disabled="disabledjc"
            >
              <el-option
                v-for="item in formulaOptions"
                :key="item.id"
                :label="item.gsMc"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计算公式" v-if="normType == 'rckh_zb'" prop="normGs">
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normGs"
              placeholder="请选择计算公式"
              :disabled="disabledjc"
            >
              <el-option
                v-for="item in formulaOptions1"
                :key="item.id"
                :label="item.gsMc"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            label="指标单位"
            v-if="normType == 'htrw_zb' || normType == 'rckh_zb'"
            prop="normDw"
          >
            <el-input
              v-model="dataForm.normDw"
              placeholder="指标单位"
              style="width: 90%"
              :disabled="disabledjc"
            ></el-input>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="24" v-if="normType !== 'rckh_zb'">
              <el-col :span="10" style="padding: 0">
                <el-form-item label="加扣范围" prop="normjka">
                  <el-input-number v-model="dataForm.normjka" :max="10000" :disabled="disabledjkfw"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="1" :pull="1" style="margin-left: 19px">至</el-col>
              <el-col :span="10" :pull="5">
                <el-form-item prop="normjkb">
                  <el-input-number v-model="dataForm.normjkb" :max="10000" :disabled="disabledjkfw"></el-input-number>
                </el-form-item>
              </el-col>
            </el-col>
          </el-row>
          <el-form-item label="指标序号" prop="normPx">
            <el-input-number v-model="dataForm.normPx" :min="1" :max="10000" placeholder="指标序号"></el-input-number>
          </el-form-item>
          <el-form-item
            label="指标规则"
            v-if="targetList.length > 0 && normType == 'rckh_zb'"
            prop="normRule"
          >
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normRule"
              placeholder="请选择指标规则"
            >
              <el-option
                v-for="item in targetList"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="数据获取方式"
            v-if="dataFromList.length > 0 && normType == 'rckh_zb'"
            prop="normGetDate"
          >
            <el-select
              style="width: 90%"
              :clearable="true"
              v-model="dataForm.normGetDate"
              placeholder="请选择数据获取方式"
            >
              <el-option
                v-for="item in dataFromList"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="指标值" prop="normValue">
          <el-input v-model="dataForm.normValue" placeholder="指标值" style="width: 90%;"></el-input>
          </el-form-item>-->
          <el-form-item label="指标说明" prop="normSm">
            <el-input
              type="textarea"
              v-model="dataForm.normSm"
              placeholder="指标说明"
              rows="5"
              style="width: 90%"
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- </el-row> -->
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmitAndCopy()" v-if="canSubmit">确定并复制</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getObj,
  addObj,
  putObj,
  addObjAndCopy,
  putObjAndCopy
} from "@/api/admin/sysnorm";
import { remote as remoteDict } from "@/api/admin/dict";
import { getFormula} from "@/api/hetongrenwu/hetongyeji/hetongzb";

import { getFormula1 } from "@/api/dailyExamine/targetExamine/createAssessment";

export default {
  data() {
    return {
      //topTitle: "新增",
      normTypeIndex: "",
      zbfl: [
        {
          label: "评分项指标库",
          value: "cp-pfx"
        },
        {
          label: "评价等次指标库",
          value: "cp-pjdc"
        },
        {
          label: "评分规则指标库",
          value: "cp-pfgz"
        },
        {
          label: "合同任务指标库",
          value: "htrw_zb"
        },
        {
          label: "精准激励指标库",
          value: "rckh_zb"
        }
      ],
      normLxs: [],
      normFls: [
        {
          label: "通用指标",
          value: "ty"
        },
        {
          label: "个性指标",
          value: "gx"
        },
        {
          label: "奖惩",
          value: "jc"
        }
      ],
      normFls1: [
        {
          label: "通用指标",
          value: "ty"
        },
        {
          label: "个性指标",
          value: "gx"
        }
      ],
      visible: false,
      canSubmit: false,
      disabledjc: false,
      dataForm: {
        normId: 0,
        normType: "",
        normName: "",
        normValue: 0,
        normSm: "",
        normPx: "",
        normLx: "",
        normDw: "",
        normFl: "",
        normGs: "",
        normjka: "",
        normjkb: ""
      },
      dataRule: {
        normType: [
          {
            required: true,
            message: "指标库不能为空",
            trigger: "blur"
          }
        ],
        normName: [
          {
            required: true,
            message: "指标名称不能为空",
            trigger: "blur"
          }
        ],
        normLx: [
          {
            required: true,
            message: "指标类型不能为空",
            trigger: "blur"
          }
        ],
        normFl: [
          {
            required: true,
            message: "指标分类不能为空",
            trigger: "blur"
          }
        ],
        normjka: [
          {
            required: true,
            message: "加扣范围不能为空",
            trigger: "blur"
          }
        ],
        normjkb: [
          {
            required: true,
            message: "加扣范围不能为空",
            trigger: "blur"
          }
        ],
        // normGs: [{
        //   required: true,
        //   message: '计算公式不能为空',
        //   trigger: 'blur'
        // }],
        // normValue: [
        //   { required: true, message: '指标值不能为空', trigger: 'blur' }
        // ],
        // normSm: [
        //   { required: true, message: '指标说明不能为空', trigger: 'blur' }
        // ],
        normPx: [
          {
            required: true,
            message: "指标序号不能为空",
            trigger: "blur"
          }
        ],
        normRule: [
          { required: true, message: "指标规则不能为空", trigger: "change" }
        ],
        normGetDate: [
          { required: true, message: "数据获取方式不能为空", trigger: "change" }
        ]
      },
      normType: "",
      formulaOptions: [],
      formulaOptions1: [],
      disabled: false,
      disabledjkfw: false,
      targetList: [],
      dataFromList: []
    };
  },
  mounted() {
    remoteDict("sys_norm_tyupe_subtype").then(response => {
      this.normLxs = response.data.data;
    });

    this.getRulesAndWayList();
  },
  methods: {
    // 获取指标规则和数据获取方式的字典
    getRulesAndWayList() {
      remoteDict("sys_norm_zbgz").then(response => {
        this.targetList = response.data.data;
      });
      remoteDict("sys_norm_sjhqfs").then(response => {
        this.dataFromList = response.data.data;
      });
    },
    normflsChange() {
      // console.log(this.dataForm.normFl)
      if (this.dataForm.normFl == "奖惩") {
        this.disabledjc = true;
        this.dataForm.normLx = "奖惩类";
        this.disabledjkfw = false;
        this.dataRule.normDw[0].required = false;
        this.dataRule.normDw[0].required = false;
      } else {
        this.disabledjc = false;
        this.dataForm.normLx = "";
        this.disabledjkfw = true;
      }
    },
    init(id, normType, zbfl, types) {
      if (id) {
        this.disabled = true;
        //this.topTitle = "修改";
      } else {
        this.disabled = false;
      }

      this.zbfl = zbfl;
      this.dataForm.normId = id || 0;
      if (normType) {
        this.normType = normType;
        this.dataForm.normType = normType;
      }
      if (normType == "rckh_zb") {
        getFormula1().then(res => {
          console.log(res);
          this.formulaOptions1 = res.data.data.records.filter(it => {
            return it.gsYz == 1;
          });
        });
      } else {
        getFormula().then(res => {
          this.formulaOptions = res.data.data;
        });
      }
      // if(types==3){
      //   this.dataForm.normFl='奖惩'
      // }
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.normId) {
          getObj(this.dataForm.normId).then(response => {
            this.dataForm = response.data.data;
            this.normflsChange();
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      if (this.dataForm.normFl == "奖惩") {
        this.dataRule.normDw[0].required = false;
        this.dataRule.normDw[0].required = false;
      }

      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.normId) {
            putObj(this.dataForm).then(data => {
              this.$notify.success("修改成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          } else {
            this.dataForm.normType = this.normType;
            addObj(this.dataForm).then(data => {
              this.$notify.success("添加成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          }
        }
      });
    },

    dataFormSubmitAndCopy() {
      if (this.dataForm.normFl == "奖惩") {
        this.dataRule.normDw[0].required = false;
        this.dataRule.normDw[0].required = false;
      }

      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.normId) {
            putObjAndCopy(this.dataForm).then(data => {
              this.$notify.success("修改成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          } else {
            this.dataForm.normType = this.normType;
            addObjAndCopy(this.dataForm).then(data => {
              this.$notify.success("添加成功");
              this.visible = false;
              this.$emit("refreshDataList");
            });
          }
        }
      });
    }
  }
};
</script>
